package com.ply.flashsalessystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ply.flashsalessystem.entity.pojo.OrderForGoods;
import com.ply.flashsalessystem.entity.pojo.OrderForGoodsDetaile;
import com.ply.flashsalessystem.entity.pojo.OrderTime;
import com.ply.flashsalessystem.entity.result.Result;
import com.ply.flashsalessystem.entity.status.OrderDetailsStatus;
import com.ply.flashsalessystem.entity.status.OrderStatus;
import com.ply.flashsalessystem.entity.vo.OrderForGoodsDetaileVo;
import com.ply.flashsalessystem.entity.vo.manage.OrderShoppingVO;
import com.ply.flashsalessystem.entity.wrapper.OrderDetailsWrapper;
import com.ply.flashsalessystem.mapper.OrderForGoodsDetaileMapper;
import com.ply.flashsalessystem.mapper.OrderForGoodsMapper;
import com.ply.flashsalessystem.mapper.OrderTimeMapper;
import com.ply.flashsalessystem.service.OrderForGoodsDetaileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ply.flashsalessystem.service.OrderForGoodsService;
import com.ply.flashsalessystem.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-23
 */
@Service
public class OrderForGoodsDetaileServiceImpl extends ServiceImpl<OrderForGoodsDetaileMapper, OrderForGoodsDetaile> implements OrderForGoodsDetaileService {
    @Autowired
    OrderForGoodsDetaileMapper orderForGoodsDetaileMapper;

    @Autowired
    OrderTimeMapper orderTimeMapper;

    @Autowired
    OrderForGoodsMapper orderForGoodsMapper;

    /**
     * 分拣查询 按照降序 排列
     * <p>
     * 按照时间 查询的
     */
    @Override
    public Page<OrderForGoodsDetaile> queryNumsForSort(Integer current, Integer limit, Integer goodId, double nums) {
        // 判断订单 是款的 条件
        QueryWrapper<OrderForGoodsDetaile> oWrapper = new QueryWrapper<>();
        oWrapper.eq("goods_id", goodId);
        oWrapper.ge("goods_order_nums", nums);
        oWrapper.orderByAsc("goods_order_nums");
        oWrapper.eq("status", OrderDetailsStatus.No_Pickup);

        // 设置时间 昨天的 订单时间
        // 配置信息 从数据库中读取
        OrderTime orderTime = orderTimeMapper.selectById(1);
        oWrapper.le("create_time", DateUtils.getOrderTime1(orderTime));
        Page<OrderForGoodsDetaile> page = page(new Page<>(current, limit), oWrapper);
        long total = page.getTotal();
        List<OrderForGoodsDetaile> records2 = new ArrayList<>();

        List<OrderForGoodsDetaile> records = page.getRecords();

        for (OrderForGoodsDetaile record : records) {
            Integer ofgId = record.getOfgId();
            OrderForGoods orderForGoods = orderForGoodsMapper.selectById(ofgId);
            if (Objects.equals(orderForGoods.getStatus(), OrderStatus.DO_PAY)){
                records2.add(record);
            }else {
                total--;
            }
        }
        page.setTotal(total);
        page.setRecords(records2);

        return page;
    }

    @Override
    public Page<OrderForGoodsDetaile> queryAll(Integer current, Integer limit, OrderDetailsWrapper orderDetailsWrapper) {
        return orderForGoodsDetaileMapper.queryAll(new Page<>(current, limit), orderDetailsWrapper);
    }


    /**
     * 修改 订单详情状态
     *
     * @param org_d_id
     * @param status
     * @return 修改完状态 后 需查看 该订单下的 所有 详情状态 是否是 打包状态
     */
    @Override
    public boolean updateOrderForGoodsDetailsStatus(Integer org_d_id, Integer status) {
        // 查询
        OrderForGoodsDetaile orderForGoodsDetaile = orderForGoodsDetaileMapper.selectById(org_d_id);
        if (orderForGoodsDetaile == null) {
            return false;
        }
        return orderForGoodsDetaileMapper.updateStatus(org_d_id, status) != 0;
    }

    /**
     * 用于打印订单 信息 采购使用   每个商品 有多少订单
     *
     * @return
     */
    //  返回两个没有什么意义 只需要返回一个 即可  并且 前提条件: 订单都是未打包的
    @Override
    public Result getTodayOrder(Integer current,Integer limit) {

        OrderTime orderTime = orderTimeMapper.selectById(1);
        Date orderTime1 = DateUtils.getOrderTime1(orderTime);

        OrderDetailsWrapper orderDetailsWrapper = new OrderDetailsWrapper();
        orderDetailsWrapper.setCreateTimeMax(orderTime1);
        orderDetailsWrapper.setStatus(OrderDetailsStatus.No_Pickup);

        Page<OrderForGoodsDetaile> page = queryAll(current, limit, orderDetailsWrapper);
        Page<OrderShoppingVO> orderShoppingVOPage = orderShopping(0, Integer.MAX_VALUE, orderTime1);

        return Result.ok()
                .message("查询采购单成功")
                .data("total", page.getTotal())
                .data("rows", page.getRecords())
                .data("orderTotal", orderShoppingVOPage.getTotal())
                .data("orderRows", orderShoppingVOPage.getRecords());
    }


    @Override
    public Page<OrderForGoodsDetaileVo> getOrderDetailsByOrderId(Integer current, Integer limit, Integer ofgId) {
        return orderForGoodsDetaileMapper.getOrderDetailsByOrderId(new Page<OrderForGoodsDetaileVo>(current, limit), ofgId);
    }


    /**
     * 采购订单  商品
     */
    public Page<OrderShoppingVO> orderShopping(Integer current, Integer limit, Date orderTime) {
        OrderDetailsWrapper orderDetailsWrapper = new OrderDetailsWrapper();
        orderDetailsWrapper.setCreateTimeMax(orderTime);
        orderDetailsWrapper.setStatus(OrderDetailsStatus.No_Pickup);
        return orderForGoodsDetaileMapper.orderShopping(new Page<>(current, limit), orderDetailsWrapper);
    }


}
